package com.soker.fan.controller;

import com.soker.fan.pojo.Fan;
import com.soker.fan.pojo.FanBarrageCartitem;
import com.soker.fan.pojo.FanClass;
import com.soker.fan.service.FanService;
import com.soker.page.Page;
import com.soker.page.PageConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class FanController {
    @Autowired
    FanService fanService;
    @Autowired
    Page page;

    //查找指定页的所有番剧
    @RequestMapping("/findAllFan")
    public String findAllFan(HttpServletRequest req, Model model, HttpSession session){
        session.setAttribute("urll",req.getRequestURI());
        Map map = commomSet(req);
        List<Fan> allFan = fanService.findAllFan(map);
        //page赋值
        page.setBeanList(allFan);
        model.addAttribute("page",page);
        List<FanClass> fanClass = fanService.findAllFanClass();
        session.setAttribute("fanClass",fanClass);
        return "fan/flist";
    }
    //查找指定页的所有番剧2
    @RequestMapping("/findAllFan2")
    public String findAllFan2(Model model){
        List<Fan> allFan = fanService.findAllFan2();
        model.addAttribute("video","false");
        model.addAttribute("allFan",allFan);
        return "popularRank";
    }
    @RequestMapping("/findAllFan3/{vname}")
    public String findAllFan3(@PathVariable String vname,Model model){
        List<Fan> allFan = fanService.findAllFanHu(vname);
        model.addAttribute("video","false");
        model.addAttribute("allFan",allFan);
        model.addAttribute("huname",vname);
        return "search";
    }
    //按风格查询
    @RequestMapping("/findByClassification")
    public String findByClassification(HttpServletRequest req, Model model, HttpSession session,int classid){
        session.setAttribute("urll","/"+classid);
        Map map = commomSet(req);
        map.put("classid",classid);
        int tr = fanService.getNumber2(map);
        List<Fan> allFan = fanService.findByClassification(map);
        //page赋值
        page.setBeanList(allFan);
        page.setTr(tr);
        model.addAttribute("page",page);
        return "fan/flist";
    }
    //按番名查询
    @RequestMapping("/findFanByFname/{fid}/{isFirst}")
    public String findFanByFname(@PathVariable String fid,Model model,HttpSession session,@PathVariable boolean isFirst){
        if(isFirst==true){
            //视频播放量+1
            fanService.updateTot(fid);
        }
        Fan fan = fanService.findFanByFname(fid);
        model.addAttribute("fan",fan);

        Map map = new HashMap();
        String uid = session.getAttribute("uid").toString();
        map.put("fid",fid);
        map.put("uid",uid);

        //查询某条视频是否被某用户点赞过
        String isZanForUser = fanService.isZanForUser(map);
        model.addAttribute("isZan",isZanForUser);

        String isBiForUser = fanService.isBiForUser(map);
        model.addAttribute("isBi",isBiForUser);

        String isZhuiForUser = fanService.isZhuiForUser(map);
        model.addAttribute("isZhui",isZhuiForUser);
        List<FanBarrageCartitem> allBarrage = fanService.findBarrageByFname(map);
        model.addAttribute("allBarrage",allBarrage);
        return "fan/fdesc";
    }
    //按番名查询2
    @RequestMapping("/findFanByFname2/{fid}")
    public String findFanByFname2(@PathVariable String fid,Model model,HttpSession session){
        Fan fan = fanService.findFanByFname(fid);
        model.addAttribute("fan",fan);
        //查询某条视频是否被某用户点赞过
        Map map = new HashMap();
        String uid = session.getAttribute("uid").toString();
        map.put("fid",fid);
        map.put("uid",uid);
        String isZhuiForUser = fanService.isZhuiForUser(map);
        if(isZhuiForUser==null){
            fanService.insertDefaultZan(map);
            isZhuiForUser = "false";
        }
        model.addAttribute("isZhui",isZhuiForUser);
        List<FanBarrageCartitem> allBarrage = fanService.findBarrageByFname(map);
        model.addAttribute("allBarrage",allBarrage);
        return "fan/fdetail";
    }
    //追番人数(排序查找)
    @RequestMapping("/findByFans/{situation}/{direction}")
    public String findByFans(@PathVariable String situation,@PathVariable String direction,Model model, HttpServletRequest req){
        Map map = commomSet(req);
        List<Fan> fans = new ArrayList<Fan>();

        if(situation.equals("findAllFan")){
            if(direction.equals("desc")){
                fans = fanService.findByFansDesc(map);
            }else{
                fans = fanService.findByFansAsc(map);
            }
        }else{
            map.put("classid",situation);
            int tr = fanService.getNumber2(map);
            page.setTr(tr);
            if(direction.equals("desc")){
                fans = fanService.findByFansDesc2(map);
            }else{
                fans = fanService.findByFansAsc2(map);
            }
        }
        //page赋值
        page.setBeanList(fans);
        model.addAttribute("page",page);
        return "fan/flist";
    }
    ///最高评分
    @RequestMapping("/findByGrade/{situation}/{direction}")
    public String findByGrade(@PathVariable String situation,@PathVariable String direction,Model model,HttpServletRequest req){
        Map map = commomSet(req);
        List<Fan> fans = new ArrayList<Fan>();

        if(situation.equals("findAllFan")){
            if(direction.equals("desc")){
                fans = fanService.findByGradeDesc(map);
            }else{
                fans = fanService.findByGradeAsc(map);
            }
        }else {
            map.put("classid",situation);
            int tr = fanService.getNumber2(map);
            page.setTr(tr);
            if(direction.equals("desc")){
                fans = fanService.findByGradeDesc2(map);
            }else{
                fans = fanService.findByGradeAsc2(map);
            }
        }
        //page赋值
        page.setBeanList(fans);
        model.addAttribute("page",page);
        return "fan/flist";
    }
    //播放数量
    @RequestMapping("/findByTotalamount/{situation}/{direction}")
    public String findByTotalamount(@PathVariable String situation,@PathVariable String direction,Model model,HttpServletRequest req){
        Map map = commomSet(req);
        List<Fan> fans = new ArrayList<Fan>();

        if(situation.equals("findAllFan")){
            if(direction.equals("desc")){
                fans = fanService.findByTotalamountDesc(map);
            }else{
                fans = fanService.findByTotalamountAsc(map);
            }
        }else {
            map.put("classid",situation);
            int tr = fanService.getNumber2(map);
            page.setTr(tr);
            if(direction.equals("desc")){
                fans = fanService.findByTotalamountDesc2(map);
            }else{
                fans = fanService.findByTotalamountAsc2(map);
            }
        }
        //page赋值
        page.setBeanList(fans);
        model.addAttribute("page",page);
        return "fan/flist";
    }
    //查找所有番剧风格(废弃)
    @RequestMapping("/findAllFanClass")
    public String findAllFanClass(Model model){
        List<FanClass> fanClass = fanService.findAllFanClass();
        model.addAttribute("fanClass",fanClass);
        return "fan/fleft";
    }
    //点赞
    @RequestMapping("/updateZan/{fid}/{uid}")
    public String updateZan(@PathVariable String fid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("fid",fid);
        map.put("uid",uid);
        String isZanForUser = fanService.isZanForUser(map);
        if (isZanForUser==null){
            fanService.insertDefaultZan(map);
        }
        fanService.chanIszan(map);
        fanService.updateZan(fid);
        return findFanByFname(fid,model,session,false);
    }
    //取消点赞
    @RequestMapping("/cancelZan/{fid}/{uid}")
    public String cancelZan(@PathVariable String fid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("fid",fid);
        map.put("uid",uid);
        fanService.chanIszan2(map);
        fanService.cancelZan(fid);
        return findFanByFname(fid,model,session,false);
    }
    //投币
    @RequestMapping("/updateBi/{fid}/{uid}")
    public String updateBi(@PathVariable String fid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("fid",fid);
        map.put("uid",uid);
        String isBiForUser = fanService.isBiForUser(map);
        if(isBiForUser==null){
            fanService.insertDefaultZan(map);
        }

        fanService.chanIsbi(map);
        fanService.updateBi(fid);
        return findFanByFname(fid,model,session,false);
    }
    //插入评论
    @RequestMapping("/insertBarrage/{fid}/{uid}/{barrage}")
    public String insertBarrage(@PathVariable String fid,@PathVariable int uid,@PathVariable String barrage,
                                Model model,HttpSession session){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        Map map = new HashMap();
        map.put("fid",fid);
        map.put("uid",uid);
        map.put("barrage",barrage);
        map.put("date",dateString);
        fanService.insertBarrage(map);
        return findFanByFname(fid,model,session,false);
    }
    //追番
    @RequestMapping("/updateZhui/{fid}/{uid}")
    public String updateZhui(@PathVariable String fid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("fid",fid);
        map.put("uid",uid);
        String isZhuiForUser = fanService.isZhuiForUser(map);
        if(isZhuiForUser==null){
            fanService.insertDefaultZan(map);
        }
        fanService.chanIszhui(map);
        fanService.updateZhui(fid);
        return findFanByFname(fid,model,session,false);
    }
    //取消追番
    @RequestMapping("/cancelZhui/{fid}/{uid}")
    public String cancelZhui(@PathVariable String fid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("fid",fid);
        map.put("uid",uid);
        fanService.chanIszhui2(map);
        fanService.cancelZhui(fid);
        return findFanByFname(fid,model,session,false);
    }
    @RequestMapping("/updateZhui2/{fid}/{uid}")
    public String updateZhui2(@PathVariable String fid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("fid",fid);
        map.put("uid",uid);
        fanService.chanIszhui(map);
        fanService.updateZhui(fid);
        return findFanByFname2(fid,model,session);
    }
    //取消追番
    @RequestMapping("/cancelZhui2/{fid}/{uid}")
    public String cancelZhui2(@PathVariable String fid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("fid",fid);
        map.put("uid",uid);
        fanService.chanIszhui2(map);
        fanService.cancelZhui(fid);
        return findFanByFname2(fid,model,session);
    }
    //抽取共同部分
    private Map commomSet(HttpServletRequest req){
        int pc = getPc(req);
        String url = getUrl(req);
        //查找数量(总共条目数)
        int tr = fanService.getNumber();
        //每页数量
        int ps = PageConstants.Fan_Page_Size;
        //查找指定页的所有番剧
        Map map = new HashMap();
        map.put("start",(pc-1)*ps);
        map.put("end",ps);
        //赋值
        page.setTr(tr);//总共条目数
        page.setPs(ps);//每页条目数
        page.setPc(pc);//设置当前页
        page.setUrl(url);//路径
        return map;
    }
    //获取当前页码
    private int getPc(HttpServletRequest req) {
        int pc = 1;/*初始化为第1页*/
        String param = req.getParameter("pc");
        if(param!=null&&!param.trim().isEmpty()) {
            try {
                pc = Integer.parseInt(param);//假设添加"ABC",报异常不处理，赋值失败，pc = 1
            }catch(RuntimeException e){}
        }
        return pc;
    }
    /* 获得url
     * http://localhost:8080/goods/BookServlet?method=findByBname&&bname=xxx请求网址
     * /goods/BookSevlet + method=findByBname&&bname=xxx*/
    private String getUrl(HttpServletRequest req) {
        String url;

        if (req.getQueryString()==null){
            url = req.getRequestURI()+"?";
        }else{
            url = req.getRequestURI()+"?"+req.getQueryString();
        }
        //+req.getQueryString();
        /*
         * 如果url中存在pc参数，截取掉，如果不存在那就不用截取
         */
        int index = url.lastIndexOf("&pc=");
        if(index!=-1) {
            url = url.substring(0,index);
        }
        return url;
    }
}
